package com.admin.controller;


import com.admin.entity.Group;
import com.admin.entity.Station;
import com.admin.entity.TGroup;
import com.admin.requestBody.GroupRequest;
import com.admin.service.IGroupService;
import com.admin.service.IStationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.bean.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *  组
 * @author marisheng
 * @since 2019-11-21
 */
@RestController
@RequestMapping("/admin/group")
public class GroupController {

    @Autowired
    private IGroupService iGroupService;

    @Autowired
    private IStationService iStationService;

    /**
     * 根据id删除
     * @return
     */
    @RequestMapping(value = "remove/groupById",method = RequestMethod.DELETE)
    public ResponseBean removeGroupC(@RequestBody TGroup tGroup){

        boolean b2 = ifRemoveGroup(tGroup.getGroupId());

        if(b2==false){
            return new ResponseBean(500,"操作失败","请输入删除的条件,和检查该分组是否还有站点在使用");
        }

        boolean b1 = iGroupService.updateById(tGroup);

        return ResponseBean.vo(b1,0);
    }

    /**
     * 新增分组
     * @param group
     * @return
     */
    @RequestMapping(value = "add/group")
    public ResponseBean addGroup(TGroup group){
        ResponseBean responseBean1 = ifGroup(group);

        if(responseBean1!=null){
            return responseBean1;
        }

        int i = iGroupService.addGroupS(group);

        return ResponseBean.vo(i,0);
    }

    /**
     * 查询所有分组
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "query/groupAll",method = RequestMethod.GET)
    public ResponseBean queryGroupAllC(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit,@RequestParam Map map){

        IPage iPage = iGroupService.queryGroupAll(page, limit,map);
        return new ResponseBean(0,iPage.getTotal(),iPage.getRecords());
    }

    @RequestMapping(value = "update/groupById",method = RequestMethod.PUT)
    public ResponseBean updateByIdGroupC(@RequestBody TGroup tGroup){

        //System.out.println(tGroup);

        ResponseBean responseBean = ifGroupById(tGroup);

        if(responseBean!=null){
            return responseBean;
        }

        boolean f = iGroupService.updateById(tGroup);

        return ResponseBean.vo(f,0);
    }

    /**
     * 用来下拉框选择框 站点的分组
     * @param stationId
     * @return
     */
    @RequestMapping(value = "/stationGroup",method = RequestMethod.GET)
    public ResponseBean stationGroup(@RequestParam(required = false) Integer stationId){
//        if (stationId==null||stationId==0){
//            return new ResponseBean(500,"操作失败","站点Id不能为空");
//        }
        List<TGroup> groups=iGroupService.stationGroup(stationId);
        if (groups.size()==0){
            return new ResponseBean(500,"操作失败","无数据");
        }
        return new ResponseBean(200,"success",groups);

    }

    /**
     * 用于删除，时的参数判断
     * @param id
     * @return
     */
    public boolean ifRemoveGroup(Integer id){

        if(id==null){
            return false;
        }

        boolean b = iGroupService.removeByIdGroupS(id);

        return b;
    }

    /**
     *  用于修改，分组的 参数判断
     * @param tGroup
     * @return
     */
    public ResponseBean ifGroupById(TGroup tGroup){

        ResponseBean responseBean=null;

//        if(tGroup.getGroupStatus()==null && (tGroup.getGroupStatus().equals("禁用") || tGroup.getGroupStatus().equals("启用"))){
//            return  responseBean=new ResponseBean(500,"操作失败","请输入分组状态,或输入正确的状态");
//        }

        if ("禁用".equals(tGroup.getGroupStatus())){
          //  tGroup.setGroupStatus("启用");
        }else if ("启用".equals(tGroup.getGroupStatus())){
           //tGroup.setGroupStatus("禁用");
        }

        if(tGroup.getGroupName()!=null){

            TGroup old = iGroupService.getById(tGroup.getGroupId());
            QueryWrapper q = new QueryWrapper();
            q.eq("group_station_id",old.getGroupStationId());
            List<TGroup> list = iGroupService.list(q);
            for (TGroup group : list) {
                if(tGroup.getGroupId()!=group.getGroupId() && tGroup.getGroupName().equals(group.getGroupName())){
                    return new ResponseBean(500,"操作失败","分组名称已被使用");
                }
            }
        }

        return responseBean;
    }

    /**
     *  自增分组参数的判断,和以防有人非法乱改数据
     * @param tGroup
     * @return
     */
    public ResponseBean ifGroup(TGroup tGroup){


        if(tGroup==null || "".equals(tGroup.getGroupName())){
            return new ResponseBean(500,"操作失败","分组名称为空");
        }

        if(tGroup.getGroupName()!=null){
            QueryWrapper q = new QueryWrapper();
            q.eq("group_station_id",tGroup.getGroupStationId());
            List<TGroup> list = iGroupService.list(q);

            for (TGroup group : list) {
                if(tGroup.getGroupName().equals(group.getGroupName())){
                    return new ResponseBean(500,"操作失败","分组名称已被使用");
                }
            }
        }


        if (tGroup.getGroupStationId()==null){
            return  new ResponseBean(500,"操作失败","请输入所属站点");
        }else if(tGroup.getGroupStationId()!=null){
            Station station = iStationService.queryByIdStationId(tGroup.getGroupStationId());
            if(station==null){
                return  new ResponseBean(500,"操作失败","请输入正确的站点");
            }
        }else if(tGroup.getGroupName()==null){
            return  new ResponseBean(500,"操作失败","请输入分组名称");
        }else if(tGroup.getGroupStatus()==null && (tGroup.getGroupStatus().equals("禁用") || tGroup.getGroupStatus().equals("启用"))){
            return  new ResponseBean(500,"操作失败","请输入分组状态,或输入正确的状态");
        }



        return null;
    }

    /**
     * 终端列表 的分组 使用
     * @param
     * @return
     */
    @RequestMapping(value = "/terminalGroupQiYong",method = RequestMethod.GET)
    public ResponseBean terminalGroupQiYong(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit,@RequestParam("groupStationId") Integer groupStationId,@RequestParam(required = false) String groupName){

        if (groupStationId==0){
            return  new ResponseBean(500,"操作失败","请输入正确的站点");
        }
        GroupRequest request=new GroupRequest(groupStationId,groupName);

        IPage iPage =iGroupService.terminalGroupQiYong(page,limit,request);
        if (iPage.getTotal()==0){
            return new ResponseBean(500,"操作失败","无数据");
        }
        return new ResponseBean(0,iPage.getTotal(),iPage.getRecords());
    }

//
//    /**
//     * 用来下拉框选择框 站点的分组
//     * @param stationId
//     * @return
//     */
//    @RequestMapping(value = "/stationGroup",method = RequestMethod.GET)
//    public ResponseBean stationGroup(Integer stationId){
//        if (stationId==null||stationId==0){
//            return new ResponseBean(500,"操作失败","站点Id不能为空");
//        }
//        List<TGroup> groups=iGroupService.stationGroup(stationId);
//        if (groups.size()==0){
//            return new ResponseBean(500,"操作失败","无数据");
//        }
//        return new ResponseBean(200,"success",groups);
//
//    }

}


